package com.example.meituan.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.meituan.activity.bean.ShopBean;
import com.example.meituan.activity.bean.TypeBean;
import com.example.meituan.activity.bean.UserAddressBean;
import com.example.meituan.db.DBUntil;

import java.util.ArrayList;
import java.util.List;

public class UserAddressDao {

    private static SQLiteDatabase db = DBUntil.con;

    // 插入新的用户地址
    public static int insertUserAddress(UserAddressBean address) {
        ContentValues values = new ContentValues();
        values.put("s_id", address.getS_id());
        values.put("s_user_id", address.getS_user_id());
        values.put("s_name", address.getS_name());
        values.put("s_phone", address.getS_phone());
        values.put("s_address", address.getS_address());
        try{
            db.insert("d_user_address", null, values);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    // 更新现有的用户地址
    public static int updateUserAddress(UserAddressBean address) {
        ContentValues values = new ContentValues();
        values.put("s_user_id", address.getS_user_id());
        values.put("s_name", address.getS_name());
        values.put("s_phone", address.getS_phone());
        values.put("s_address", address.getS_address());
        try{
            db.update("d_user_address", values, "s_id=?", new String[]{address.getS_id()});
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    // 删除指定的用户地址
    public static int deleteUserAddress(String addressId) {
        try{
            db.delete("d_user_address", "s_id=?", new String[]{addressId});
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    // 根据主键ID获取用户地址
    public static UserAddressBean getUserAddressById(String addressId) {
        Cursor cursor = db.query("d_user_address", null, "s_id=?", new String[]{addressId}, null, null, null);

        UserAddressBean address = null;
        if (cursor.moveToFirst()) {
            address = UserAddressBean.fromCursor(cursor);
        }
        cursor.close();
        return address;
    }


    // 根据用户ID获取所有用户地址
    public static List<UserAddressBean> getUserAddressesByUserId(String userId) {
        Cursor cursor = db.rawQuery("SELECT * FROM d_user_address WHERE s_user_id = ?",new String[]{userId});

        List<UserAddressBean> userAddressBeanList =new ArrayList<>();
        while (cursor.moveToNext()) {
            @SuppressLint("Range") String id = cursor.getString(cursor.getColumnIndexOrThrow("s_id"));
            @SuppressLint("Range") String address = cursor.getString(cursor.getColumnIndexOrThrow("s_address"));
            @SuppressLint("Range") String phone = cursor.getString(cursor.getColumnIndexOrThrow("s_phone"));
            @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("s_name"));
            UserAddressBean userAddressBean=new UserAddressBean();
            userAddressBean.setS_id(id);
            userAddressBean.setS_user_id(userId);
            userAddressBean.setS_address(address);
            userAddressBean.setS_phone(phone);
            userAddressBean.setS_name(name);
            userAddressBeanList.add(userAddressBean);
        }

        cursor.close();

        return userAddressBeanList;
    }

    // 删除所有用户地址
    public int deleteAllUserAddresses() {
        return db.delete("d_user_address", null, null);
    }

    // 统计用户地址记录总数
    public int countUserAddresses() {
        Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM d_user_address", null);
        int count = 0;
        if (cursor != null && cursor.moveToFirst()) {
            count = cursor.getInt(0);
            cursor.close();
        }
        return count;
    }
}
